package com.hangyutech.strelitzia.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.hangyutech.strelitzia.config.AliYunConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 557 strelitzia.
 *
 * @author WiconWang <WiconWang@gmail.com>
 * @copyright 2020/12/4 4:38 PM
 */
@Component
public class AliyunSmsUtils {
    Logger logger = LoggerFactory.getLogger(AliyunSmsUtils.class);
    @Autowired
    private AliYunConfig aliyunSmsSendProperty;

    public String sendSMS(String phone, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunSmsSendProperty.getAccessKeyId(), aliyunSmsSendProperty.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();

        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", aliyunSmsSendProperty.getSignName());
        request.putQueryParameter("TemplateCode", aliyunSmsSendProperty.getKey1TemplateCode());
        request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

}